//
//  XMWebViewController.swift
//  AnneKit-Swift
//
//  Created by 寜小陌 on 2021/3/11.
//  Copyright © 2021 宁小陌. All rights reserved.
//

import UIKit
import WebKit

class XMWebViewController: UIViewController, WKNavigationDelegate{
    private weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        var webView = WKWebView()
        let preferences = WKPreferences()
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
        let userContentController = WKUserContentController()
        configuration.userContentController = userContentController
        self.webView = webView
        self.webView.navigationDelegate = self
        webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), configuration: configuration)
        self.view.addSubview(self.webView)
        self.webView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        
        let filePath = Bundle.main.path(forResource: "index", ofType: "html")
        guard let path = filePath else {
            return
        }
        
        guard let url = URL(string: path) else {
            return
        }
        
        let request = URLRequest(url: url)
        
        self.webView.load(request)
        
        if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
            let request = URLRequest(url: url)
            self.webView.load(request)
        }
    
    }
    
    // 页面开始加载时调用
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        
    }
    // 当内容开始返回时调用
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        
    }
    // 页面加载完成之后调用
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        // 设置localStorage
        let jsString = "sessionStorage.setItem('lastName','2021-03-11 14:18:32 存储')"
        // 移除localStorage
        // NSString *jsString = @"localStorage.removeItem('userContent')";
        // 获取localStorage
        // NSString *jsString = @"localStorage.getItem('userContent')";
//        [self.webView stringByEvaluatingJavaScriptFromString:jsString];
        self.webView.evaluateJavaScript(jsString, completionHandler: nil)
    }
    // 页面加载失败时调用
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        
    }
    // 接收到服务器跳转请求之后调用
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        
    }
    // 在收到响应后，决定是否跳转 -> 默认允许
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        //允许跳转
        decisionHandler(.allow)
        //不允许跳转
//        decisionHandler(.cancel)
    }
    // 在发送请求之前，决定是否跳转 -> 默认允许
    @available(iOS 13.0, *)
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
        decisionHandler(.allow, preferences)
    }
}

